﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using TallyBook.BLL.Manger.Expend;
using TallyBook.BLL.Service;
using TallyBook.BLL.Service.Imp;

namespace TallyBook.UI
{
    public partial class OutlayUpdateForm : Form
    {
        private readonly IOutlayService<Outlay> _outlayService;
        private readonly List<TextBox> _textBoxs = new List<TextBox>();
        public Outlay Outlay = new Outlay();

        public OutlayUpdateForm(IOutlayService<Outlay> outlayService)
        {
            _outlayService = outlayService;
            InitializeComponent();
        }

        public Action RefeshMainForm { get; set; }

        private void OutlayUpdateForm_Load(object sender, EventArgs e)
        {

            txtId.Text = Outlay.AccountTypeId.ToString(CultureInfo.InvariantCulture);
            txtCurrentMoney.Text = Outlay.Money.ToString(CultureInfo.InvariantCulture);
            txtComment.Text = Outlay.Comment;
            _textBoxs.Add(txtId);
            _textBoxs.Add(txtCurrentMoney);
            _textBoxs.Add(txtComment);
            Member();

            comboBox1.SelectedItem = Outlay.OutLayTypeId;
            comboBox2.SelectedItem = Outlay.AccountTypeId;
        }

        public void Member()
        {
            var account = new AccountService();
            comboBox2.DataSource = null;
            List<AccountType> getaccount = account.GetName();
            comboBox2.DataSource = getaccount;
            comboBox2.DisplayMember = "Name";
            comboBox2.ValueMember = "Id";

            var income = new OutlayService();
            comboBox1.DataSource = null;
            List<OutLayType> getincome = income.GetName();
            comboBox1.DataSource = getincome;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "Id";
        }

        private void btnOutlayUpdate_Click(object sender, EventArgs e)
        {

            if (_textBoxs.Any(item => string.IsNullOrEmpty(item.Text)))
            {
                MessageBox.Show(@"信息未填完整");
                return;
            }

            var detection = new Detection();
            var list = _textBoxs.Where(item => item.Name != "txtComment").ToArray();
            if (list.Any(detection.DetectionMoney))
            {
                MessageBox.Show(@"含非法字符");
                return;
            }

            long accounttypeid = long.Parse(comboBox2.SelectedValue.ToString());
            long outlaytypeid = long.Parse(comboBox1.SelectedValue.ToString());
            long outlayid = long.Parse(txtId.Text);
            decimal money = decimal.Parse(txtCurrentMoney.Text);
            string comment = txtComment.Text;
            DateTime time = DateTime.Now;

            if (_outlayService.Update(accounttypeid, outlaytypeid, outlayid, money, comment, time) != 0)
            {
                MessageBox.Show(@"添加成功");
                RefeshMainForm();
                Hide();
            }
        }

        private void btnOutlayReset_Click(object sender, EventArgs e)
        {
            txtId.Text = null;
            txtCurrentMoney.Text = null;
            txtCurrentMoney.Text = null;
        }
    }
}